package cn.iocoder.yudao.module.ride.controller.admin.bindlog;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ride.controller.admin.bindlog.vo.*;
import cn.iocoder.yudao.module.ride.dal.dataobject.bindlog.BindLogDO;
import cn.iocoder.yudao.module.ride.service.bindlog.BindLogService;

@Tag(name = "管理后台 - 车辆绑定/解绑记录")
@RestController
@RequestMapping("/ride/bind-log")
@Validated
public class BindLogController {

    @Resource
    private BindLogService bindLogService;

    @PostMapping("/create")
    @Operation(summary = "创建车辆绑定/解绑记录")
    @PreAuthorize("@ss.hasPermission('ride:bind-log:create')")
    public CommonResult<Long> createBindLog(@Valid @RequestBody BindLogSaveReqVO createReqVO) {
        return success(bindLogService.createBindLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新车辆绑定/解绑记录")
    @PreAuthorize("@ss.hasPermission('ride:bind-log:update')")
    public CommonResult<Boolean> updateBindLog(@Valid @RequestBody BindLogSaveReqVO updateReqVO) {
        bindLogService.updateBindLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除车辆绑定/解绑记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ride:bind-log:delete')")
    public CommonResult<Boolean> deleteBindLog(@RequestParam("id") Long id) {
        bindLogService.deleteBindLog(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除车辆绑定/解绑记录")
                @PreAuthorize("@ss.hasPermission('ride:bind-log:delete')")
    public CommonResult<Boolean> deleteBindLogList(@RequestParam("ids") List<Long> ids) {
        bindLogService.deleteBindLogListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得车辆绑定/解绑记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ride:bind-log:query')")
    public CommonResult<BindLogRespVO> getBindLog(@RequestParam("id") Long id) {
        BindLogDO bindLog = bindLogService.getBindLog(id);
        return success(BeanUtils.toBean(bindLog, BindLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得车辆绑定/解绑记录分页")
    @PreAuthorize("@ss.hasPermission('ride:bind-log:query')")
    public CommonResult<PageResult<BindLogRespVO>> getBindLogPage(@Valid BindLogPageReqVO pageReqVO) {
        PageResult<BindLogDO> pageResult = bindLogService.getBindLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BindLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出车辆绑定/解绑记录 Excel")
    @PreAuthorize("@ss.hasPermission('ride:bind-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBindLogExcel(@Valid BindLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BindLogDO> list = bindLogService.getBindLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "车辆绑定/解绑记录.xls", "数据", BindLogRespVO.class,
                        BeanUtils.toBean(list, BindLogRespVO.class));
    }

}